﻿<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <meta
            name="viewport"
            content="width=device-width, initial-scale=1, maximum-scale=1" />
        <title>临海妇保-自定义表单</title>
        <link rel="stylesheet" href="./layui/css/layui.css" />
        <link rel="stylesheet" href="./ayq/modules/formDesigner.css" />
        <link rel="stylesheet" href="./ayq/modules/cron.css" />
        <link rel="stylesheet" href="./ayq/modules/labelGeneration.css" />
     
        <style>
      .centered {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%; /* 或者指定一个具体的高度 */
      }
    </style>
    </head>

    <body>
        <div id="app" class="layui-container">
            <div class="layui-row">
                <div class="layui-col-lg-offset4">
                    <h1>{{ title }}</h1>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-md12" id="testdemo"></div>
            </div>

        </div>
        <script src="vue.js"></script>
        <script type="text/javascript" src="./layui/layui.js"></script>
        <script
            type="text/javascript"
            src="./ayq/modules/Sortable/Sortable.js"></script>
        <script
            type="text/javascript"
            src="./ayq/modules/iceEditor/iceEditor.js"></script>
    
        <script src="axios.min.js"></script>
        <script type="text/javascript">

      const app = new Vue({
          el: "#app",
          data: {
              title: "test",
              formjson: "",
              formId: "",
              recordId:""
          },
          methods: {
              GetItems: function () {
                  const url = new URL(window.location.href);
                  const params = new URLSearchParams(url.search);
                  let formId = params.get('formId');
                  this.formId = formId;
                  let recordId = params.get("recordId");
                  if (recordId) {
                      this.recordId = recordId;
                      this.GetRecordByRecordId(recordId);
                  }
               
                  if (formId) {
                      this.GetFormItemById(formId);
                  }
              },

              GetRecordByRecordId: function (recordId) {
                  if (recordId != undefined) {
                      let url = "/api/app/form-instance/" + recordId;
                      axios.get(url).then(response => {
                          //console.log(response)
                          let formid = response.data.formItemId;
                          this.GetFormItemById(formid);
                          localStorage.setItem("current_content", response.data.content)
                      })
                  }
              },
              GetFormItemById: function (formId) {
                  if (formId != undefined) {
                      localStorage.setItem("form:formId", formId);
                      this.formId = formId;
                      let url = "/api/app/form-item/" + formId;
                      axios.get(url).then(response => {
                          let form = response.data;
                          this.title = form.formName;
                          this.formjson = form.formContent.replace(/&quot;/g, '"');
                          window.localStorage.setItem('layui_form_json', this.formjson);
                          app.title = form.formName;

                      });
                  }

              }
          },
          mounted: function () {
              localStorage.setItem("current_content", null);
              this.GetItems();
          }
      });


      var demojs = [];
      //JavaScript代码区域
      layui.config(
          { base: './ayq/modules/' }).use(['formDesigner', 'form', 'layer', 'upload'], function () {
              var layer = layui.layer;
              var $ = layui.jquery;
              var upload = layui.upload;
              var index = layui.index;
              var formDesigner = layui.formDesigner;
              var form = layui.form;
              var render;
              if (window.localStorage.getItem('layui_form_json') !== undefined) {
                  demojs = JSON.parse(window.localStorage.getItem('layui_form_json'));
                  render = formDesigner.render({
                      elem: '#testdemo',
                      data: demojs,
                      viewOrDesign: true,
                      formData: JSON.parse(localStorage.getItem("current_content"))
                  });
                  var images = render.getImages();
                  for (var i = 0; i < images.length; i++) {
                      upload.render({
                          elem: '#' + images[i].select
                          , url: '' + images[i].uploadUrl + ''
                          , multiple: true
                          , before: function (obj) {
                              layer.msg('图片上传中...', {
                                  icon: 16,
                                  shade: 0.01,
                                  time: 0
                              })
                          }
                          , done: function (res) {
                              layer.close(layer.msg());//关闭上传提示窗口
                              //上传完毕
                              $('#uploader-list-' + item.id).append(
                                  '<div id="" class="file-iteme">' +
                                  '<div class="handle"><i class="layui-icon layui-icon-delete"></i></div>' +
                                  '<img style="width: 100px;height: 100px;" src=' + res.data.src + '>' +
                                  '<div class="info">' + res.data.title + '</div>' +
                                  '</div>'
                              );
                          }
                      });
                  }

                  var filesData = render.getFiles();
                  for (var i = 0; i < filesData.length; i++) {
                      upload.render({
                          elem: '#' + filesData[i].select
                          , elemList: $('#list-' + filesData[i].select) //列表元素对象
                          , url: '' + filesData[i].uploadUrl + ''
                          , accept: 'file'
                          , multiple: true
                          , number: 3
                          , auto: false
                          , bindAction: '#listAction-' + filesData[i].select
                          , choose: function (obj) {
                              var that = this;
                              var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                              //读取本地文件
                              obj.preview(function (index, file, result) {
                                  var tr = $(['<tr id="upload-' + index + '">'
                                      , '<td>' + file.name + '</td>'
                                      , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                                      , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                                      , '<td>'
                                      , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                                      , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                                      , '</td>'
                                      , '</tr>'].join(''));

                                  //单个重传
                                  tr.find('.demo-reload').on('click', function () {
                                      obj.upload(index, file);
                                  });

                                  //删除
                                  tr.find('.demo-delete').on('click', function () {
                                      delete files[index]; //删除对应的文件
                                      tr.remove();
                                      uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                                  });

                                  that.elemList.append(tr);
                                  element.render('progress'); //渲染新加的进度条组件
                              });
                          }
                          , done: function (res, index, upload) { //成功的回调
                              var that = this;
                              //if(res.code == 0){ //上传成功
                              var tr = that.elemList.find('tr#upload-' + index)
                                  , tds = tr.children();
                              tds.eq(3).html(''); //清空操作
                              delete this.files[index]; //删除文件队列已经上传成功的文件
                              return;
                              //}
                              this.error(index, upload);
                          }
                          , allDone: function (obj) { //多文件上传完毕后的状态回调
                              console.log(obj)
                          }
                          , error: function (index, upload) { //错误回调
                              var that = this;
                              var tr = that.elemList.find('tr#upload-' + index)
                                  , tds = tr.children();
                              tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                          }
                          , progress: function (n, elem, e, index) {
                              element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
                          }
                      });
                  }
              }


              //document.getElementsByName("zyhm")[0].addEventListener("keydown", function (event) {
              //    if (event.key == 'Enter') {
              //        event.preventDefault(); // 阻止默认行为
              //        event.stopPropagation(); // 阻止事件冒泡
              //        let InputParam = document.getElementsByName("zyhm")[0].value;
              //        let remoteUrl = "http://192.168.0.30:7000/lhfy/app/zybrry/info/" + InputParam + "?" + String( Date.now());
              //        axios.get(remoteUrl).then(Response => {
              //            let result = Response.data.zyBrry;
              //            if (result) {
              //                render.setFormData(result);
              //            }


              //        })
              //        return;
              //    }
              //})


              //监听提交
              form.on('submit(demo1)', function (event) {
                  event.preventDefault(); // 阻止默认行为
                  event.stopPropagation(); // 阻止事件冒泡

                  var json = render.getFormData();
                  //  console.log(json);
                
                  const record = {
                      content: JSON.stringify(json),
                      formItemId: app.formId
                  };
                  if (app.recordId) {
                      record.Id = app.recordId;
                  }

                  axios.post("/api/app/FormInstances", record).then(r => {
                   
                      try {
                          window.location.href = "/forminstances";
                        
                      } catch (e) {

                      }

                    
                  }).catch(x => {
                      //alert(x);
                  })
                  return false;
              });
            });


            function generateUUID() {
                // 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', y是 8,9,A,B 中的一个，x是随机的十六进制数  
                let dt = new Date().getTime();
                const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                    const r = (dt + Math.random() * 16) % 16 | 0;
                    dt = Math.floor(dt / 16);
                    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
                });
                return uuid;
            }  
 
          </script>
    </body>
</html>